<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" id="username1" name="username1" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button id="search" type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>



        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                    <input type="checkbox" name="statusopen"  value = {{d.status}} mid={{d.id}}   lay-skin="switch" lay-filter="switchTest" {{d.status=="1"?"checked":""}} lay-text="启用|禁用" >
                </div>
            </div>
        </script>
        <script id="addform" type="text/html">
            <form class="layui-form" action="" lay-filter="empform">
                <input id="id" name="id" type="hidden">
                <div class="layui-form layuimini-form">
                    <div class="layui-form-item">
                        <label class="layui-form-label required">用户名</label>
                        <div class="layui-input-block">
                            <input type="text"  name="username" lay-verify="required" lay-reqtext="用户名不能为空"
                                   placeholder="请输入用户名" value="" class="layui-input">

                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">性别</label>
                        <div class="layui-input-block">
                            <input type="radio" name="sex" value="男" title="男" checked="">
                            <input type="radio" name="sex" value="女" title="女">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label required">密码</label>
                        <div class="layui-input-block">
                            <input type="password" name="password" lay-verify="required" lay-reqtext="密码不能为空"
                                   placeholder="请输入密码" value="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">手机号：</label>
                        <div class="layui-input-block">
                            <input type="number" name="tel" placeholder="请输入要绑定的手机" value="" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <input type="button" id="savebtn" class="layui-btn" value="确认保存"/>
                        </div>
                    </div>
                </div>
            </form>
            </form>    </script>

    </div>
</div>
<script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script th:inline="none">
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;
        $("#search").on("click", function () {
            table.render({
                elem: '#currentTableId',
                url: '/member/list',
                toolbar: '#toolbarDemo',
                where:{keyword:$("#username1").val()},

                cols: [[
                    {type: "checkbox", width: 50},
                    {field: 'id', width: 80, title: 'ID', sort: true},
                    {field: 'username', width: 100, title: '用户名'},
                    {field: 'sex', width: 80, title: '性别', sort: true},
                    {field: 'tel', title: '手机号', minWidth: 150},
                    {field: 'nicename', width: 100, title: '昵称'},
                    {field: 'status', width: 80, title: '状态'},
                    {field: 'code', minWidth: 80, title: '会员ID'},
                    {field: 'avatar', width: 80, title: '头像'},
                    {field: '启用/禁用', minWidth: 80, title: '启用/禁用' ,toolbar: '#currentTableBar', align: "center"}

                ]],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                skin: 'line'
            })
        }).click();
        form.render();

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                layer.open({
                    type: 1,
                    content: $("#addform").html(), //把html放进script里
                    area: ['400px', '400px']
                });
                form.render();
                $("#savebtn").on("click", function () {
                    var data = form.val("empform");
                    if (!data.name == null || data.name == "") {
                        layer.msg("用户名为空");
                        return false;
                    }
                    $.post("/user/save", data, function (result) {
                        console.log(data);
                        if (result.code == 0) {
                            layer.closeAll();
                            $("#search").click();//点击刷新
                        } else {
                            layer.msg(result.msg);
                        }
                    });
                });

            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                var ids=[];
                $.each(data,function (i,v) {
                    ids[i]=v.id;
                });
                layer.confirm('你真的要删这些吗？', function () {
                    $.post("/user/deleteByPrimaryKeys", {ids: ids}, function () {
                        layer.closeAll();
                        $("#search").click();//点击刷新
                    });
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        form.on('switch(switchTest)',function(obj) {
            this.checked?obj.value=1:obj.value=0;
            var id = $(this).attr('mid');
            console.log(id);
            var data =new Object();
            data.id=id;
            if(obj.value==0){
                $.post("/member/off",{id:data.id},function () {
                    $("#search").click();//点击刷新
                });
            }else {
                $.post("/member/on",{id:data.id},function() {
                    $("#search").click();//点击刷新
                })
            }
        })

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {

                $.post("/user/edit",{id:obj.data.id},function(s) {
                    if (s.code == 0) {
                        layer.open({
                            type: 1,
                            content: $("#addform").html(), //把html放进script里
                            area: ['400px', '400px']
                        });
                        form.render();
                        $("#savebtn").on("click", function () {
                            var data = form.val("empform");
                            if (!data.username == null || data.username == "") {
                                layer.msg("用户名为空");
                                return false;
                            }
                            $.post("/user/save", data, function (result) {
                                console.log(data);
                                if (result.code == 0) {
                                    layer.closeAll();
                                    $("#search").click();//点击刷新
                                } else {
                                    layer.msg(result.msg);
                                }
                            });
                        });
                        form.val("empform", s.data);
                    } else {
                        layer.msg(s.msg);
                    }
                });
            } else if (obj.event === 'delete') {
                layer.confirm('你真的要删吗？', function () {
                    $.post("/user/delete",{id:obj.data.id},function(){
                        layer.closeAll();
                        $("#search").click();
                    });
                });
            }
        });

    });
</script>

</body>
</html>